function success(val) {
    // layer.msg(val,
    //     {
    //         icon: 1,
    //         time: 2000,
    //         anim: 1,
    //         offset: "60px",
    //         area: 'auto',
    //         // maxWidth: '10px',
    //         // maxHeight: '30px',
    //         shade: 0
    //     });
    Notiflix.Notify.Success(val);
}

function failure(val) {
    // layer.msg(val,
    //     {
    //         icon: 2,
    //         time: 2000,
    //         anim: 1,
    //         offset: "60px",d
    //         area: 'auto',
    //         // maxWidth: '10px',
    //         // maxHeight: '30px',
    //         shade: 0
    //     });
    Notiflix.Notify.Failure(val);
}

Notiflix.Notify.Init({
    cssAnimationStyle: 'zoom',
    cssAnimationDuration: 500,
    plainText: false
});

Notiflix.Confirm.Init({
    backgroundColor: "#F8F8F8",
    borderRadius: "10px",
    messageColor: "#d84c32",
    backOverlayColor: "rgba(0,0,0,0.5)"
});


$(function () {
    load_name();
})

function load_name() {
    $.get({
        url: '/index/getName',
        success: function (result) {
            if (result.status === 200) {
                $('#real-name').text(result.data);
            }
        }
    })
}

function download(fileName, url) {
    let xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);        // 也可以使用POST方式，根据接口
    xhr.setRequestHeader("Authorization", window.localStorage.getItem("Authorization"));
    xhr.responseType = "blob";    // 返回类型blob
    // 定义请求完成的处理函数，请求前也可以增加加载框/禁用下载按钮逻辑
    xhr.onload = function () {
        success("文件下载中...");
        // 请求完成
        if (this.status === 200) {
            // 返回200
            let blob = this.response;
            if (blob.type === 'application/json') {
                const reader = new FileReader();
                reader.readAsText(blob, 'utf-8');
                reader.onload = function (e) {
                    const result = reader.result;
                    console.log(result)
                    const parseObj = JSON.parse(result);
                    failure(parseObj.msg)
                }
                return;
            }
            if (blob.size === 0) {
                failure("没有可下载的附件");
                return;
            }
            success("文件下载成功！！");
            let reader = new FileReader();
            reader.readAsDataURL(blob);    // 转换为base64，可以直接放入a表情href
            reader.onload = function (e) {
                // 转换完成，创建一个a标签用于下载
                let a = document.createElement('a');
                a.download = fileName;
                a.href = e.target.result;
                $("body").append(a);    // 修复firefox中无法触发click
                a.click();
                $(a).remove();
            }
        }
    };
    // 发送ajax请求
    xhr.send()
}

//开启编辑
function open_edit(target) {
    let val = $(target).parents('tr').prop("outerHTML");
    $(target).parents('.ds').siblings().not(".ds").prop('contenteditable', 'true');
    //恢复其他按钮
    // $(target).siblings().toggleClass('disabled');
    //禁用当前按钮
    $(target).addClass('disabled');
    // //禁用批量修改按钮
    // $('.ui.button.checkModify').addClass('disabled');

    // $('.edit-button').not(target).addClass('disabled');
    return val;
}

//管理页面编辑
function admin_open_edit(target) {
    let td = $(target).parent().parent().parent();
    let val = td.parent().prop("outerHTML");

    td.siblings().not(".ds").not(".role").not('.department').prop('contenteditable', 'true');
    //恢复其他按钮
    $(target).parent().siblings().children().removeClass('disabled');
    //禁用当前按钮
    $(target).toggleClass('disabled');
    return val;
}

function verify_number(val) {
    return /^\d+$/.test(val);
}

function verify_work_order_number(val) {
    return !/^([PBF][\d]+)$/.test(val);
}

//校验模板名称
function verify_template_name(val) {
    return !/^([a-zA-Z0-9.\-_()\u4E00-\u9FA5]+)$/.test(val);
}

function load() {
    return layer.load(0, {
        shade: false
    });
}

//关闭编辑
function close_edit(val, target) {
    $(target).parents('tr').replaceWith(val);
    // console.log($(this).parent().siblings().text());
    $(target).parents('.ds').siblings().removeAttr('contenteditable');

    $(target).prev().removeClass('disabled');
    // //禁用编辑按钮
    // $(target).prev().toggleClass('disabled');
    // //恢复批量修改按钮
    // $('.ui.button.checkModify').toggleClass('disabled');
    //
    // $('.edit-button').not(target).removeClass('disabled');
}

function admin_close_edit(val, target) {
    let td = $(target).parent().parent().parent()
    td.parent().replaceWith(val);
    // console.log($(this).parent().siblings().text());
    td.siblings().removeAttr('contenteditable');
    //禁用编辑按钮
    $(target).parent().prev().children().toggleClass('disabled');
    $('.ui.dropdown').dropdown({
        on: 'hover'
    });
}


//开启批量更新编辑
function open_batch_update() {
    if ($('tbody').children().length > 1) {
        let val = $('tbody').prop("outerHTML");
        $("input[name='checkIt']").toggleClass('hide').prop('checked', false).parent().siblings().not(".ds").prop('contenteditable', $('a#select_all').hasClass('hide'));
        $('a#select_all').toggleClass('hide');
        $('.ui.button.sureCheck').toggleClass('disabled');
        $('.ui.button.cancelCheck').toggleClass('disabled');
        $('.ui.button.edit-button').toggleClass('disabled');
        return val;
    }
}

function check_number(val) {
    return /^\d+$/.test(val);
}

function verify_soft_version(val) {
    return !/^[A-Za-z.0-9]{6,50}$/.test(val);
}

//取消批量更新
function close_batch_update(target) {
    $('tbody').replaceWith(tbodyContent);
    $('a#select_all').toggleClass('hide');
    // $("input[name='checkIt']").prop('checked',false).siblings().removeAttr('contenteditable');
    $(target).toggleClass('disabled');
    $('.ui.button.sureCheck').toggleClass('disabled');
}

//恢复其他按钮
function recover(target) {
    //隐藏全选
    $('a#select_all').toggleClass('hide');
    //恢复编辑按钮
    $('.ui.button.edit-button').toggleClass('disabled');
    //禁用取消按钮
    $('.ui.button.cancelCheck').toggleClass('disabled');
    //禁用当前按钮
    $(target).toggleClass('disabled');
    //隐藏复选框，移除td可编辑
    $("input[name='checkIt']").toggleClass('hide').prop('checked', false).parent().siblings().prop('contenteditable', false);
}

//时间格式验证 yyyy-MM-dd
function verify_date_format(val) {
    let reg = new RegExp("(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])");
    return reg.test(val);
}


function verify_time(time) {
    return /^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^(([^0][0-9]+|0)$)|^(([1-9]+)\.([0-9]{1,2})$)|^(([1-9]+)$)/.test(time);
}

//渲染tfoot
function renderTfoot(size, data, method, value, cur) {
    let tfoot = $('tfoot')
    tfoot.html("");
    let colspan = $('thead').children().first().children().length;
    let nums = data.navigatepageNums;
    let foot = "<tr><th colspan='" + colspan + "' style='text-align: center'><div class='ui pagination menu table-foot'>";
    if (data.hasPreviousPage) {
        foot += "<a class='item search-wo' onclick='" + method + "(" + 1 + "," + size + ",\"" + value + "\")'>首页</a>"
    }
    let num = Math.min(nums.length, 10);
    if (nums.length > 1) {
        for (let i = 0; i < num; i++) {
            if (nums[i] === cur) {
                foot += "<a class='item active search-wo' onclick='" + method + "(" + nums[i] + "," + size + ",\"" + value + "\")'>" + nums[i] + "</a>"
            } else {
                foot += "<a class='item search-wo' onclick='" + method + "(" + nums[i] + "," + size + ",\"" + value + "\")'>" + nums[i] + "</a>"
            }
        }
    }
    if (data.hasNextPage) {
        foot += "<a class='item search-wo' onclick='" + method + "(" + data.pages + "," + size + ",\"" + value + "\")'>尾页</a>"
    }
    foot += "</div></th></tr>";
    // foot += `<a class="item" onclick="queryAllWorkOrder(${result.data.pages},20)">尾页</a></th></tr></div>`;
    if (nums.length > 1) {
        tfoot.html(foot);
    }
}

function package_menu(data, target) {
    target.empty();
    $.each(data, function (index, item) {
        target.append("<div class='item' data-value='" + item + "'><span>" + item + "</span></div>")
    })
}

function package_template_menu(data, target) {
    target.empty();
    $.each(data, function (index, item) {
        target.append("<div class='item' data-value='" + item.templateId + "'><span>" + item.templateName + "</span></div>");
    });
}

function package_menu_plus(data, target) {
    target.empty();
    $.each(data, function (index, item) {
        if (index < 2) {
            target.append("<div class='item' data-tooltip='" + item.vs + "' data-position='bottom center' data-value='" + item.ks + "'><span>" + item.ks + "</span></div>")
        } else {
            target.append("<div class='item' data-tooltip='" + item.vs + "' data-position='top center' data-value='" + item.ks + "'><span>" + item.ks + "</span></div>")
        }
    })
}

function show_modal(target) {
    target.modal({
        closable: false
    }).modal("show");
}

// const tail = "<button class=\"ui positive animated button edit-button\">\n" +
//     "                                <div class=\"visible content\">编辑</div>\n" +
//     "                                <div class='hidden content'>\n" +
//     "                                    <i class='edit outline icon'></i>\n" +
//     "                                </div>\n" +
//     "                            </button>\n" +
//     "                            <button class=\"ui olive animated button cancel-button disabled\">\n" +
//     "                                <div class=\"visible content\">取消</div>\n" +
//     "                                <div class=\"hidden content\">\n" +
//     "                                    <i class=\"reply icon\"></i>\n" +
//     "                                </div>\n" +
//     "                            </button>\n" +
//     "                            <button class=\"ui primary animated button save-button disabled\">\n" +
//     "                                <div class=\"visible content\">保存</div>\n" +
//     "                                <div class=\"hidden content\">\n" +
//     "                                    <i class=\"save outline icon\"></i>\n" +
//     "                                </div>\n" +
//     "                            </button>";
//
// const tail2 = "<button class='ui olive animated button anti-closing'>\n" +
//     "                                <div class='visible content'>反结案</div>\n" +
//     "                                <div class='hidden content'>\n" +
//     "                                    <i class='reply icon'></i>\n" +
//     "                                </div>\n" +
//     "                            </button>";
//
// const tail3 = "<button class='ui olive animated button case-closed'>\n" +
//     "                                <div class='visible content'>结案</div>\n" +
//     "                                <div class='hidden content'>\n" +
//     "                                    <i class='reply icon'></i>\n" +
//     "                                </div>\n" +
//     "                            </button>" +
//     "<button class='ui olive animated button anti-finished'>\n" +
//     "                                <div class='visible content'>反完工</div>\n" +
//     "                                <div class='hidden content'>\n" +
//     "                                    <i class='reply icon'></i>\n" +
//     "                                </div>\n" +
//     "                            </button>";
//
// const deleteBtn = "<button class='ui olive animated button delete-wo'>\n" +
//     "                                <div class='visible content'>删除</div>\n" +
//     "                                <div class='hidden content'>\n" +
//     "                                    <i class='reply icon'></i>\n" +
//     "                                </div>\n" +
//     "                            </button>";
//
// const signBtn = "<button class='ui red animated button approval-view'>\n" +
//     "                                <div class='visible content'>审批查看</div>\n" +
//     "                                <div class='hidden content'>\n" +
//     "                                    <i class='eye icon'></i>\n" +
//     "                                </div>\n" +
//     "                            </button>";
//
// const tail_plus = tail + deleteBtn;
//
// function get_tails(val) {
//     let tails;
//     switch (val) {
//         case "完工":
//             tails = tail3 + signBtn;
//             break;
//         case "结案":
//             tails = tail2 + signBtn;
//             break;
//         case "下达":
//             tails = tail_plus;
//             break;
//         case "异常":
//             tails = tail;
//             break;
//         case "在制":
//             tails = tail;
//             break;
//     }
//     return tails;
// }
